package com.example.demo;

import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentSkipListSet;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.json.JsonTest;
import com.example.json.User;

/**
 *   测试控制层
 * @author Administrator
 *
 */
@RestController
@RequestMapping("/test")
public class TestController {
	
	public static Set<String> set = new ConcurrentSkipListSet<>();
	
	@Autowired
	private HttpServletRequest request;
	
	@GetMapping("/getUser1")
	public String getUser(String name, int age) {
		name = name + "帅";
		age = age -2;
		return "name:" + name + ", age:" + age;
	}
	
	@PostMapping("/getUser2")
	public String getUser2(Map map) {
		String name = (String)map.get("name");
		name = name + "帅";
		return "name:" + name ;
	}
	
	@PostMapping(value = "/getUser3")
	public String getUser3(@RequestBody Map<String, Object> userInfo, HttpServletRequest request) {
		String parameter = request.getParameter("t");
		System.out.println(parameter + "dddddd");
		String name = userInfo.get("name") + "帅";
		Integer age = Integer.valueOf(userInfo.get("age").toString()) -2;
		return "name:" + name + ", age:" + age;
	}
	
	@PostMapping(value = "/getUser4/{t}")
	public String getUser4(@RequestBody Map<String, Object> userInfo, @PathVariable String t) {
		t = t + "dddd";
		String name = userInfo.get("name") + "帅";
		Integer age = Integer.valueOf(userInfo.get("age").toString()) -2;
		return "name:" + name + ", age:" + age;
	}
	
	@GetMapping("/getUser5/{name}")
	public User getUser5(@PathVariable String name) throws Exception {
		System.out.println(name);
		User u = JsonTest.test1();
		return u;
	}
	
	/**
	 * 直接使用实体，只能处理简单类型参数
	 * @param u
	 * @throws Exception
	 */
	@PostMapping("/getUser6")
	public void getUser6(User u) throws Exception {
		System.out.println(u);
	}
	
	@GetMapping("/bf")
	public void getUser6(String name) throws Exception {
		// 1.自动注入获取request
		// String parameter = request.getParameter("name");
		
		// 2.手动调用获取request
		// ServletRequestAttributes servletRequest = (ServletRequestAttributes)RequestContextHolder.currentRequestAttributes();
		// HttpServletRequest request = servletRequest.getRequest();
		if (set.contains(name)) {
			System.out.println(name + "\t重复出现， request并不安全");
		} else {
			set.add(name);
			System.out.println(name);
		}
		Thread.sleep(1000);
		
	}
	

}
